#include<stdio.h>
#include<stdlib.h>
int size,size2;
int a[100],b[100];

//enter fn.
void enter()
{
int num,i;
printf("Start entering values [enter -1 to stop]\n");
while(i>=0)
{
scanf("%d",&num);
a[i]=num;
if (a[i]==-1)
break;
i++;

}
a[i+1]=-1;

}


//append fn.
void append()
{
int i,j=0,app;
printf("Enter value to append:\t");
scanf("%d",&app);
while(a[j]!=-1)
{j++;
}
a[j]=app;
}

//display fn.
void display()
{int k=0;
printf("[");
while(a[k]!=-1)
{
printf("%d\t",a[k]);
k++;
}
printf("]");
}
//extend
void extend()
{
 int i=0,j=0,pos=0,num,m=0;
 printf("Enter elements of new list [press -1 to stop]");
 while(i>=0)
{
scanf("%d",&num);
b[i]=num;
if (b[i]==-1)
break;
i++;
}
b[i+1]=-1;
while(a[j]!=-1)
{
pos++;
j++;
}

while(b[m]!=-1)
{m++;
}
size2=m;
int k;
for(k=0;k<size2;k++)
{
a[pos]=b[k];
pos++;
}
a[pos]=-1;
printf("\n List Extended !");
}



//pop
void pop()
{
int i=0,j,cnt=0,num,val;
printf("Enter the position from where value is to be removed or just enter -1 to remove from end");
scanf("%d",&num);

while(a[i]!=-1)
{
               cnt++;
               i++;
}

if (num==-1)
{
val=a[cnt];
a[cnt]=-1;
printf("Value removed from position end is %d:",val);
}
if (num==0)
printf("Wrong position !!!");

else
{
    num=num-1;
    val=a[num];
    cnt+=1;
for(j=num+1;j<cnt;j++)
{
                      a[j-1]=a[j]; 
}
    printf("Value removed from position %d is %d:",num+1,val);
}
}

//size
void find_size()
{
int i=0,j=0;
while(a[i]!=-1)
{i++;
}
size=i;

}
//count
void count()
{
int i,count=0,val;
printf("Enter value to be counted");
scanf("%d",&val);
find_size();
for(i=0;i<size;i++)
{
if(a[i]==val)
count++;
}
printf("Number of times value %d appears in this list is %d",val,count);
}

//index
void ind()
{
int x,s=-1,i=0;
printf("enter value whose index is to be determined");
scanf("%d",&x);
while(a[i]!=-1)
{
if(a[i]==x)
s=i;
i++;
}
if (s==-1)
printf("Value not in list!");
else
printf("Index value of %d is %d",x,s);

}
//insert
void insert()
{
   int pos,i,k,val,index,ns;
   printf("Enter position");
   scanf("%d",&pos);
   printf("\nEnter Value");
   scanf("%d",&val);
   index=pos-1;
   find_size();
   for(i=size-1;i>=index;i--)
   {
      a[i+1]=a[i];
      }
a[index]=val;
a[size+1]=-1;
  
}
//remove
void rem()
{
int r,ri,i=0,c=0;
printf("Enter value to be removed");
scanf("%d",&r);
find_size();
while (a[i]!=-1)
{
      if (a[i]==r)
      {
      c++;
      ri=i;
      for(i=ri+1;i<size;i++)
      {
      a[i-1]=a[i];
      }
      a[i-1]=-1;
      }
 i++;     
}
if(c!=0)
printf("value removed");
else
printf("value to be removed not found");
}

//sort
void sort()
{
int x,y,temp;
find_size();
for(x=0; x<size; x++)

{

for(y=0; y<size-1; y++)

{

if(a[y]>a[y+1])

{

temp = a[y+1];

a[y+1] = a[y];

a[y] = temp;

}

}

}

printf("list is Sorted now!");

                                      
}



//reverse
void reverse()
{
int c[100];
int i,j,k,temp;
find_size();
for (j=0,i=size-1;j<size,i>=0;j++,i--)
{
c[i]=a[j];
}
for (k=0; k<size; k++)
a[k]=c[k];
                                     
                   
printf("List is now reversed");

}


void main()
{

int a1[100],opt,opt2,i=0,num;
do 
{
system("cls");

printf("Enter your Choice number:");
printf(" \n0.Enter values \n1.Extend \n2.Pop \n3.Append \n4.Count \n5.Index \n6.Insert \n7.Remove \n8.Sort \n9.Reverse \n10.Display \n:>");  
scanf("%d",&opt);
switch(opt)
{
case 0: enter();
break;
case 1: extend();
break;
case 2: pop();
break;
case 3: append();
break;
case 4: count();
break;
case 5: ind();
break;
case 6: insert();
break;
case 7: rem();
break;
case 8: sort();
break;
case 9: reverse();
break;
case 10: display();
break;
}
printf("\nPress 1. to continue");
scanf("%d",&opt2);
}while(opt2==1);
}
